package com.augurit.smartform.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.smartform.entity.SmComponentRegister;
import com.augurit.smartform.web.component.form.ComponentRegisterForm;

/**
 * 智能表单组件注册DAO类
 */
@Repository
public class SmComponentRegisterDao extends BaseDao<SmComponentRegister,Long>{
	/**
	 * 获取系统所有组件注册目录
	 * @return
	 */
	public List<SmComponentRegister> getAllComponentRegister(){
		String hql="from SmComponentRegister where type = '1'";
		return this.find(hql, new Object[]{});
	}
	
	/**
	 * 取得组件注册文件
	 * @return
	 */
	public SmComponentRegister getRegisterFile(){
		String hql="from SmComponentRegister where type = '0'";
		return this.findUnique(hql, new Object[]{});
	}
	
	/**
	 * 添加初始化的注册文件
	 * @return
	 */
	public SmComponentRegister initRegisterFile(){
		StringBuffer sb=new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<componentlist>");
		sb.append("</componentlist>");
		String content=sb.toString();
		
		SmComponentRegister register=new SmComponentRegister();
		register.setType("0");
		register.setContent(content);
		this.save(register);
		return register;
	}
	
	/**
	 * 通过组件目录名称进行查找
	 * @return
	 */
	public SmComponentRegister queryByName(String name){
		String hql="from SmComponentRegister where type = '1' and name='"+name+"'";
		return this.findUnique(hql, new Object[]{});
	}
	
	/**
	 * 通过HQL更新数据库
	 * @param form
	 */
	public void updateByHql(ComponentRegisterForm form){
		String hql="update SmComponentRegister set name=:name,type=:type,content=:content,available=:available where id = "+form.getId();
		this.getSession().createQuery(hql).setProperties(form).executeUpdate();
	}
	
	/**
	 * 通过HQL禁用组件
	 * @param form
	 */
	public void disableByHql(ComponentRegisterForm form){
		String hql="update SmComponentRegister set available=0 where id = "+form.getId();
		this.batchExecute(hql);
	}
}
